angular.module('Controller')

.controller('QgyHouseManageCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    $scope.pick={
        apartmentId:$localStorage['accountStore']
        buildingId:undefined
        buildingData:undefined
        floor:undefined
        floorData:undefined
        floorEditable:false
        room:undefined
    }
    loadApartment=()->
        $scope.apartmentList=[]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
                if $scope.apartmentList.length>0 and $scope.pick.apartmentId==undefined
                    $scope.pick.apartmentId=$scope.apartmentList[0].id
                $scope.loadBuilding()
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    _.extend $scope,
        resetHouse:()->
            $scope.floatHouseInfo=undefined
            $scope.pick.houseId=undefined
            $scope.houseList=[]
        resetBuilding:()->
            $scope.pick.buildingId=undefined
            $scope.resetFloor()
        resetFloor:()->
            $scope.pick.floor=undefined
            $scope.floorList=[]
            $scope.resetHouse()
        loadBuilding:()->
            $scope.buildingList=[]
            reqData={page: 0,size: 999,apartmentId:$scope.pick.apartmentId,sort:'id,ASC'}
            $http.get('/building',params:reqData)
            .success (resp)->
                if resp.code==0
                    $scope.buildingList=$scope.buildingList.concat(resp.message.content)
                    $scope.pick.buildingId=$scope.buildingList[0].id
                    $scope.loadFloor()
        loadFloor:()->
            if $scope.pick.buildingId==undefined
                $scope.resetFloor()
            else
                $scope.resetFloor()
                building=_.find($scope.buildingList,(theBd)->theBd.id==$scope.pick.buildingId)
                $scope.floorList=$scope.floorList.concat(building.floorDesc) if building
                $scope.pick.floor=$scope.floorList[0].id
                $scope.pickFloor()
        loadHouse:()->
            if $scope.pick.floor==undefined
                $scope.resetHouse()
                return
            $scope.resetHouse()
            reqData={
                page: 0,size: 999,
                apartmentId:$scope.pick.apartmentId
                buildingId:$scope.pick.buildingId
                floor:$scope.pick.floor
                enabled:true
            }
            $http.get('/house',params:reqData)
            .success (resp)->
                console.log('house',resp)
                if resp.code==0
                    $scope.houseList=resp.message.content
#                    _.defer(()->
#                        setTimeout(()->
#                            $('.ab-house-item').mouseover((ele)->
#                                houseId=Number(ele.currentTarget.id.replace('house-item-',''))
#                                tmpHouse=_.find($scope.houseList,(item)->item.id==houseId)
#                                $scope.mouseoverHouse(tmpHouse)
#                            )
#                        ,1000)
#                    )

        pickBuilding:()->
            tmp=_.find($scope.buildingList,(theBd)->theBd.id==$scope.pick.buildingId)
            if tmp
                $scope.pick.buildingData=angular.copy(tmp)
                $scope.pick.floor=undefined
                $scope.pick.floorData=undefined
                $scope.pick.floorEditable=false
                $scope.pick.room=undefined
                list=[]
                $scope.floorList=list.concat($scope.pick.buildingData.floorDesc)
        pickApartment:()->
            $scope.resetBuilding()
            $scope.loadBuilding()
        pickFloor:()->
            tmp=_.find($scope.floorList,(theFl)->theFl.id==$scope.pick.floor)
            if tmp
                $scope.pick.floorData=angular.copy(tmp)
                $scope.loadHouse()
            ''
        pickHouse:()->
            if $scope.pick.houseId==undefined
                $scope.houseInfo==undefined
                $scope.pick.payType=undefined
                $scope.payWayList=[{value:undefined,name:'--'}]
                $scope.leaseList=[{value:undefined,name:'--'}]
            else
                $scope.houseInfo=_.find($scope.houseList,(theRoom)->theRoom.id==$scope.pick.houseId)
                console.log('houseInfo',$scope.houseInfo)
                $scope.payWayList=commonConstant.transferpayWayList($scope.houseInfo.payType)
                $scope.leaseList=commonConstant.leaseList()
                $scope.pick.lease=$scope.leaseList[1].value
                if $scope.payWayList.length>0
                    $scope.pick.payWay=$scope.payWayList[0].value
        roomStyle:(item)->
            style={
                "width" : item.width+'px',
                "height" : item.height+'px',
                "line-height":item.height+'px',
                "left" : item.left+'px',
                "top" : item.top+'px',
                "border":if item.id==$scope.pick.roomData?.id then '2px solid yellowgreen' else 'none'
            }
            return style
        mouseoverHouse:(item)->
            if item and item.id!=$scope.floatHouseInfo
                $scope.floatHouseInfo=item
                theWid=300
                left=item.left+item.width/2-theWid/2
                left=parseInt(left)
                if left<0
                    left=0
                else if left>960-theWid
                    left=960-theWid
                $scope.floatHouseInfo.ngStyle={
                    left:left+'px'
                    top:(item.top+item.height)+'px'
                }
                $http.get("/contract/house/#{$scope.floatHouseInfo.id}/statusInfo")
                .success((resp)->
                    if resp.code==0
                        data=resp.message
                        return if $scope.floatHouseInfo==undefined
                        $scope.floatHouseInfo.contractId=data.contractId
                        $scope.floatHouseInfo.contractStatus=data.contractStatus
                        $scope.floatHouseInfo.contractStatusName=data.contractStatusName
                        $scope.floatHouseInfo.customerCard=data.customerCard
                        $scope.floatHouseInfo.customerName=data.customerName
                        $scope.floatHouseInfo.customerPhone=data.customerPhone
                        $scope.floatHouseInfo.houseId=data.houseId
                        $scope.floatHouseInfo.lastLeaseTime=data.lastLeaseTime
                        $scope.floatHouseInfo.leaseEnd=data.leaseEnd
                        $scope.floatHouseInfo.leaseStart=data.leaseStart
                        $scope.floatHouseInfo.nextLeaseTime=data.nextLeaseTime
                        $scope.floatHouseInfo.payWay=data.payWay
                        $scope.floatHouseInfo.payWayName=data.payWayName
                        $scope.floatHouseInfo.peopleNum=data.peopleNum
                        $scope.floatHouseInfo.contractRental=data.contractRental
                        $scope.floatHouseInfo.contractDeposit=data.contractDeposit
                        $scope.floatHouseInfo.depositBillId=data.depositBillId
                        $scope.floatHouseInfo.sex=data.sex
                )
        clearFloatInfo:()->
            $scope.floatHouseInfo=undefined
        toPayWayStr:(payWay)->
            list=commonConstant.transferpayWayList(payWay)
            str=''
            _.each list,(theW)->
                str+=theW.name+'/'
            str=str.substr(0,str.length-1) if str.length>0
            str
        doOffCheckIn:(item)->
            $rootScope.pageType='offline-check'
            $rootScope.pageParams={house:item}
            menuManage.goState('offline-check-in',true)
        doOnCheckIn:(item)->
            $rootScope.pageType='online-check'
            $rootScope.pageParams=item
            menuManage.goState('online-check-in',true)
        doOffLease:(item)->
            $rootScope.pageType='off-lease-confirm'
            $rootScope.pageParams={houseId:item.id}
            menuManage.goState('off-lease-confirm',true)
        earnestCheckIn:(item)->
            reqData={houseId:item.id,page:0,size:999}
            $http.get("/earnest",params:reqData)
            .success((resp)->
                if resp.code==0 and resp.message.content.length==1
                    earnest=resp.message.content[0]
                    $rootScope.pageType='offline-check'
                    $rootScope.pageParams={house:item,earnest:earnest}
                    menuManage.goState('offline-check-in',true)
                else
                    wfAlert.toastError('定金信息获取失败')
            )
        goHouseDetail:(item)->
            $rootScope.pageType='house-detail'
            $rootScope.pageParams={houseId:item.id}
            menuManage.goState('house-detail',true)
        doRefresh:()->
            $scope.needRefresh=false
            $scope.loadHouse()
    $rootScope.$on('refresh',(info)->
        if $scope.needRefresh!=true
            $scope.needRefresh=true
    )
    $rootScope.$watch('navTabs.active',(href)->
        if(href=='house-manage' && $scope.needRefresh)
            $scope.doRefresh()
    )
)